package com.atguigu.gulimall.product.service.impl;

import com.atguigu.gulimall.product.dao.CategoryBrandRelationDao;
import net.bytebuddy.asm.Advice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;

import com.atguigu.gulimall.product.dao.CategoryDao;
import com.atguigu.gulimall.product.entity.CategoryEntity;
import com.atguigu.gulimall.product.service.CategoryService;


@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, CategoryEntity> implements CategoryService {

    @Autowired
    private CategoryBrandRelationDao  categoryBrandRelationDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryEntity> page = this.page(
                new Query<CategoryEntity>().getPage(params),
                new QueryWrapper<CategoryEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<CategoryEntity> listWithTree() {
        //1、获取所有商品
        List<CategoryEntity> allProducts = baseMapper.selectList(null);

        //2、对商品进行级别分类
        //2.1找出所有一级分类
        List<CategoryEntity> res = allProducts.stream().filter((CategoryEntity) ->
                CategoryEntity.getParentCid() == 0
        ).map((menu) -> {
            menu.setChildrens(findChildrens(menu, allProducts));
            return menu;
        }).sorted((menu1, menu2) -> {
            return (menu1.getSort() == null ? 0 : menu1.getSort()) - (menu2.getSort() == null ? 0 : menu2.getSort());
        }).collect(Collectors.toList());
        return res;
    }

    @Override
    public void removeMenusByIds(List<Long> asList) {
        //TODO 1、检查当前删除的菜单，是否被别的地方引用
        baseMapper.deleteBatchIds(asList);
    }

    @Override
    public Long[] findParentPath(Long catelogId) {
        CategoryEntity categoryEntity = baseMapper.selectById(catelogId);
        List<Long> parentPath = new ArrayList<Long>();
        List<Long> parentCategoryPath = findParentCategoryPath(catelogId, parentPath);
        Collections.reverse(parentCategoryPath);
        return parentPath.toArray(new Long[parentPath.size()]);
    }

    @Override
    public void updateDetail(CategoryEntity category) {
       // categoryService.updateById(category);
        baseMapper.updateById(category);

        //更新品牌何分类表中的关联信息
        categoryBrandRelationDao.updateCategoryName(category.getCatId(),category.getName());
    }

    private List<Long> findParentCategoryPath(Long catelogId,List<Long> parentPath){
        parentPath.add(catelogId);
        CategoryEntity categoryEntity = baseMapper.selectById(catelogId);
        if(categoryEntity.getParentCid()!=0){
            findParentCategoryPath(categoryEntity.getParentCid(),parentPath);
        }

        return parentPath;
    }

    public List<CategoryEntity> findChildrens(CategoryEntity root,List<CategoryEntity> allProducts){
        List<CategoryEntity> chirends = allProducts.stream().filter(categoryEntity -> {
            return categoryEntity.getParentCid() == root.getCatId();
        })
                .map((menu) -> {
                    menu.setChildrens(findChildrens(menu, allProducts));
                    return menu;
                }).sorted((menu1, menu2) -> {
                    return (menu1.getSort() == null ? 0 : menu1.getSort()) - (menu2.getSort() == null ? 0 : menu2.getSort());
                }).collect(Collectors.toList());
        return chirends;
    }

}